﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineJobPortal.Models;
using System.Data.Entity;
using System.Data;

namespace JobPortal.Controllers
{
    public class UserController : Controller
    {
        JobPortalEntities7 objEntities = new JobPortalEntities7();
        IUserRepository user;

        public UserController()
        { }
        public UserController(IUserRepository user)
        {
            this.user = user;
        }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Home()
        {
            object value = Session["email"];
            if (value != null)
            {
                List<Job> joblist = objEntities.Jobs.ToList();
                ViewBag.jobsData = joblist;
                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult Categories()
        {
             object value = Session["email"];
             if (value != null)
             {
                 return View();
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult Admin_Jobs()
        {
             object value = Session["email"];
             if (value != null)
             {
                 ViewBag.jobsData = user.SearchCategory("Admin");
                 return View();
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult Marketing_Jobs()
        {
             object value = Session["email"];
             if (value != null)
             {
                 ViewBag.jobsData = user.SearchCategory("Marketing");
                 return View();
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult IT_Jobs()
        {
             object value = Session["email"];
             if (value != null)
             {
                 ViewBag.jobsData = user.SearchCategory("IT");
                 return View();
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult HR_Jobs()
        {
             object value = Session["email"];
             if (value != null)
             {
                 ViewBag.jobsData = user.SearchCategory("HR");
                 return View();
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult Accounting_Jobs()
        {
            object value = Session["email"];
            if (value != null)
            {
                ViewBag.jobsData = user.SearchCategory("Accounting");
                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }
       
        public ActionResult Job_Detail(int id)
        {
            object value = Session["email"];
            if (value != null)
            {
                Job j = user.JobDetails(id);
                ViewBag.ID = id;
                return View(j);
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult Apply()
        {
             object value = Session["email"];
             if (value != null)
             {
                 // job id
                 string id = Request["ID"];
                 ApplyforJob apply = new ApplyforJob();
                 apply.JobID = Convert.ToInt32(id);
                 object session_id = Session["userid"];
                 int userid = (int)session_id;
                 apply.UserID = userid;
                 apply.Status = "Pending";
                 user.ApplyForJob(apply);
                 return View("ApplyResult");
             }
               return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult ApplyResult()
        {
            return View();
        }
        public ActionResult ApplyResult1()
        {
            ViewModel model = new ViewModel();
            object session_id = Session["userid"];
            int userid = (int)session_id;
             object value = Session["email"];
             if (value != null)
             {
                 var emplist = from apply in objEntities.ApplyforJobs
                               join user in objEntities.Users
                               on apply.UserID equals user.UserId
                               join job in objEntities.Jobs
                               on apply.JobID equals job.JobID
                               where user.UserId == userid
                               select new ViewModel
                               {
                                   User = user,
                                   Job = job,
                                   ApplyforJob = apply
                               };
                 return View(emplist);
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult CV()
        {
            object value = Session["email"];
            if (value != null)
            {
                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }

       
    }
}
